/*
 * @Author: lll 1280388921@qq.com
 * @Date: 2025-04-13 21:59:46
 * @LastEditors: lll 1280388921@qq.com
 * @LastEditTime: 2025-04-14 11:57:12
 * @FilePath: /lesson28/server.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <iostream>
#include"comm.hpp"
#include"log.hpp"
using namespace std;


int main()
{
    Init init;
    Log log;
    // log.Enable(Screen);
    // log.Enable(Onefile);
    log.Enable(Classfile);
    

    //打开管道
    int fd=open(FIFO_FILE,O_RDONLY);
    //等待写入方打开之后，自己才会打开文件，向后执行。
    //因为要有人写才能读。反之，写端也一样。
    //open阻塞了！
    if(fd<0)
    {
        log(Fatal,"error string: %s , error code: %d\n",strerror(errno),errno);
        exit(FIFO_OPEN_ERR);
    }

    //为了测试多写点日志等级的代码
    log(Info,"server open file done , error string: %s , error code: %d\n",strerror(errno),errno);
    log(Debug,"server open file done , error string: %s , error code: %d\n",strerror(errno),errno);
    log(Warming,"server open file done , error string: %s , error code: %d\n",strerror(errno),errno);
    log(Error,"server open file done , error string: %s , error code: %d\n",strerror(errno),errno);
    log(Fatal,"server open file done , error string: %s , error code: %d\n",strerror(errno),errno);

    //开始通信
    while(1)
    {
        char buffer[1024]={0};
        int x=read(fd,buffer,sizeof(buffer));
        if(x>0)
        {
            buffer[x]=0;
            cout<<"client say: "<<buffer<<endl;
        }
        else if(x==0)
        {
            log(Debug,"client quit , me too , error string: %s , error code: %d\n",strerror(errno),errno);
            break;
        }
        else break;
    }

    close(fd);

    return 0;
}